package cn.lingyangwl.agile.infra.dict.controller;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.lingyangwl.agile.file.excel.CusExcelExportUtil;
import cn.lingyangwl.agile.infra.dict.service.DictItemService;
import cn.lingyangwl.agile.infra.module.dict.model.entity.DictItem;
import cn.lingyangwl.agile.infra.module.dict.model.rqrs.DictItemListReq;
import cn.lingyangwl.agile.model.constants.ValidationCons;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.framework.security.xss.core.XssCleanIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 数据字典信息
 * 
 * @author shenguangyang
 */
@Api(tags = "字典条目管理")
@Validated
@RestController
@RequestMapping("/dictItem")
public class DictItemController {
    @Resource
    private DictItemService dictItemService;

    @ApiOperation("分页查询")
    @GetMapping("/listItem")
    @RamResource(action = "sys:dictItem:query", name = "查询字典条目", type = ActionType.READ)
    public Resp<List<DictItem>> listItem(DictItemListReq req) {
        List<DictItem> list = dictItemService.listItem(req);
        return Resp.ok(list);
    }

    @PostMapping("/export")
    @RamResource(action = "sys:dictItem:export", name = "导出字典条目", type = ActionType.READ)
    public void export(HttpServletResponse response, DictItemListReq req) throws Exception {
        List<DictItem> list = dictItemService.listItem(req);
        ExportParams params = new ExportParams();
        params.setType(ExcelType.HSSF);
        CusExcelExportUtil.exportExcel(response, params, "字段数据.xls", DictItem.class, list);
    }

//    /**
//     * 查询字典数据详细
//     */
//    @ApiOperation("查询字典数据详细")
//    @GetMapping(value = "/{dictCode}")
//    public SingleResponse<SysDictItemPO> getInfo(@PathVariable Long dictCode) {
//        return SingleResponse.buildSuccess(dictItemService.selectDictDataById(dictCode));
//    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @ApiOperation("根据字典编码查询字典条目信息")
    @GetMapping(value = "/list")
    @RamResource(action = "sys:dictItem:query", name = "查询字典条目", type = ActionType.READ)
    public Resp<List<DictItem>> listDictItem(@NotEmpty(message = ValidationCons.NOT_EMPTY)
                                                  @RequestParam(value = "code") String dictType) {
        List<DictItem> data = dictItemService.listByType(dictType);
        return Resp.ok(data);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @ApiOperation("根据字典编码查询字典条目信息")
    @GetMapping(value = "/listByCodes")
    @RamResource(action = "sys:dictItem:query", name = "查询字典条目", type = ActionType.READ)
    public Resp<Map<String, Map<String, DictItem>>> listDictItemByCodes(@NotEmpty(message = ValidationCons.NOT_EMPTY)
                                               @RequestParam(value = "codes") List<String> dictCodes) {
        Map<String, Map<String, DictItem>> resp = dictItemService.listByTypes(dictCodes);
        return Resp.ok(resp);
    }

    @ApiOperation("根据字典编码查询字典条目信息")
    @GetMapping(value = "/listByCode")
    @RamResource(action = "sys:dictItem:query", name = "查询字典条目", type = ActionType.READ)
    public Resp<Collection<DictItem>> listDictItemByCodes(@NotEmpty(message = ValidationCons.NOT_EMPTY)
                                                                        @RequestParam(value = "code") String code) {
        Map<String, Map<String, DictItem>> resp = dictItemService.listByTypes(Collections.singletonList(code));

        return Resp.ok(resp.getOrDefault(code, Collections.emptyMap()).values());
    }

    /**
     * 新增字典类型
     */
    @ApiOperation("新增字典条目")
    @PostMapping("/save")
    @RamResource(action = "sys:dictItem:save", name = "保存字典条目", type = ActionType.WRITE)
    @XssCleanIgnore({"label"})
    public Resp<?> save(@Validated @RequestBody DictItem dictItem) {
        return dictItemService.saveDictItem(dictItem) ? Resp.ok() : Resp.fail("保存失败");
    }

    /**
     * 修改保存字典类型
     */
    @ApiOperation("修改保存字典条目")
    @PostMapping("/update")
    @RamResource(action = "sys:dictItem:update", name = "更新字典条目", type = ActionType.WRITE)
    @XssCleanIgnore({"label"})
    public Resp<?> update(@RequestBody DictItem dictItem) {
        return dictItemService.updateDictItem(dictItem) ? Resp.ok() : Resp.fail("更新失败");
    }

    /**
     * 删除字典类型
     */
    @ApiOperation("批量删除字典条目")
    @DeleteMapping("/deleteBatch")
    @RamResource(action = "sys:dictItem:delete", name = "删除字典条目", type = ActionType.WRITE)
    public Resp<?> deleteBatch(@RequestParam(value = "ids") Long[] dictCodes) {
        dictItemService.deleteByIds(dictCodes);
        return Resp.ok();
    }

    /**
     * 删除字典类型
     */
    @ApiOperation("删除字典条目")
    @DeleteMapping("/delete")
    @RamResource(action = "sys:dictItem:delete", name = "删除字典条目", type = ActionType.WRITE)
    public Resp<?> delete(@RequestParam(value = "id") @NotEmpty(message = "请输入字典id") String dictId) {
        dictItemService.deleteById(Long.valueOf(dictId));
        return Resp.ok();
    }
}